package com.example.sunmiv1.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.LossInfo;
import com.example.sunmiv1.utils.Tools;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LossInfoAdapter extends BaseAdapter {

    private List<LossInfo.DataBean> list;
    private Context context;
    private ViewHolder viewHolder;

    public LossInfoAdapter(List<LossInfo.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.loss_info_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.lossInfoName.setText(list.get(position).getGoods_name());
        if ("2".equals(list.get(position).getAttribute_state())) {
            viewHolder.lossInfoSkuName.setText(list.get(position).getSku_name());
        } else {
            viewHolder.lossInfoSkuNameLin.setVisibility(View.GONE);
        }
        Tools.hideViewInItem(list.get(position).getCompany(),viewHolder.lossInfoUnit);
        viewHolder.lossInfoNum.setText(list.get(position).getLoss_num() + "");
        viewHolder.lossInfoCode.setText(list.get(position).getGoods_code());
        viewHolder.lossInfoGoodsNum.setText(list.get(position).getGoods_number());
        viewHolder.lossInfoRemark.setText(list.get(position).getRemarks());
        if("1".equals(list.get(position).getSerial_number_state())){
            viewHolder.lossInfoNum.setTextColor(ContextCompat.getColor(context,R.color.blue_3293FA));
        }else{
            viewHolder.lossInfoNum.setTextColor(ContextCompat.getColor(context,R.color.text_666));
        }
        final int index = position;
        viewHolder.lossInfoNum.setOnClickListener(v -> {
            choose.click(index);
        });
        return convertView;
    }

    private Choose choose;
    public void setChoose(Choose choose){
        this.choose = choose;
    }

    public interface Choose{
        void click(int index);
    }

    static class ViewHolder {
        @BindView(R.id.lossInfo_name)
        TextView lossInfoName;
        @BindView(R.id.lossInfo_skuName)
        TextView lossInfoSkuName;
        @BindView(R.id.lossInfo_skuNameLin)
        LinearLayout lossInfoSkuNameLin;
        @BindView(R.id.lossInfo_num)
        TextView lossInfoNum;
        @BindView(R.id.lossInfo_code)
        TextView lossInfoCode;
        @BindView(R.id.lossInfo_goodsNum)
        TextView lossInfoGoodsNum;
        @BindView(R.id.lossInfo_unit)
        TextView lossInfoUnit;
        @BindView(R.id.lossInfo_remark)
        TextView lossInfoRemark;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
